package com.business.system.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.business.base.action.BaseAction;
import com.business.base.tools.JsonUtil;
import com.business.system.pojo.User;
import com.business.system.service.IUserSevice;
import com.utility.Page;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
public class UserAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	@Autowired
	private IUserSevice userService;
	private int id;

	@Action(value = "UserAction_loginIn")
	// 或者写成 @Action("login")
	public String loginIn() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			user = (User) JsonUtil.getObject4JsonString(paramsCode, User.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			User userTemp = userService.findByAccountAndPassword(
					user.getAccount(), user.getPassword());
			if (userTemp == null) {
				bool = false;
			} else {
				bool = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "UserAction_listUser")
	public String listUser() throws Exception {

		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			user = (User) JsonUtil.getObject4JsonString(paramsCode, User.class);
		} else {
			user = new User();
		}

		Page<User> page = this.userService.findUserListPage(this.user,
				this.pageNo, this.limit);
		List<User> list = page.getResults();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + page.getTotalRecord() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "UserAction_allUser")
	public String allUser() throws Exception {
		List<User> list = this.userService.selectAll();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + list.size() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "UserAction_addUser")
	public String addUser() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			user = (User) JsonUtil.getObject4JsonString(paramsCode, User.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.userService.insert(user);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "UserAction_editUser")
	public String editUser() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			user = (User) JsonUtil.getObject4JsonString(paramsCode, User.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.userService.update(user);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "UserAction_deleteUser")
	public String deleteUser() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.userService.delete(id);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	// @Action(value = "add", results = { @Result(name = "success", location =
	// "/index.jsp") })
	public String other() throws Exception {
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}
